﻿using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace Minecraft_Armor_Stand_Creater.Util
{
    class VersionData : Singleton<VersionData>
    {
        /// <summary>
        /// 当前版本号
        /// </summary>
        public readonly string version = "Version:" + ConfigurationManager.AppSettings["MainVersion"];

        /// <summary>
        /// 更新日志链接
        /// </summary>
        readonly static string updatelog = ConfigurationManager.AppSettings["UpdateLog"];

        /// <summary>
        /// 更新器下载链接
        /// </summary>
        public readonly string updaterlink = "http://minecraft-adaptable-studio-1254149191.coscd.myqcloud.com/MAS%20Updater.exe";

        readonly StreamReader streamReader = new(WebRequest.Create(updatelog).GetResponse().GetResponseStream(), Encoding.UTF8);

        /// <summary> 
        /// 最新版本信息检测 & 新版本信息弹出
        /// </summary>
        public void SearchVersion()
        {
            Thread th = new(new ThreadStart(delegate
            {
                string NewVerStr, NewVer = "", Infor = "";
                try //读取网页流到末尾，即使用正则表达式从网页流中提取，没有则为空
                {
                    NewVerStr = streamReader.ReadLine();
                    int strl = ("Version:").Length;
                    NewVer = NewVerStr.Substring(strl, NewVerStr.Length - strl);//读取版本号
                    Infor = streamReader.ReadToEnd();//读取剩余内容
                    Log.Write("版本检测完成", LogType.Info);

                    //Version Messager

                    //Dispatcher.Invoke(new ThreadStart(delegate
                    //{
                    //校验版本号
                    if (version != NewVerStr)
                    {
                        //主页检测提示
                        //VersionText.Content = FindResource("FindNewVer") + NewVer;
                        //VerPath.Data = FindResource("Icon.Warning") as Geometry;
                        //VerPath.Fill = new SolidColorBrush(Color.FromRgb(255, 0, 0));

                        //弹窗提示
                        SingleControls.Instance.VersionMessager.Version.Content = NewVer;
                        SingleControls.Instance.VersionMessager.UpdateLog.Text = Infor;
                        SingleControls.Instance.VersionMessager.Show();
                    }
                    //else
                    //{
                    //    //VersionText.Content = FindResource("LatestVer");
                    //    //VerPath.Data = FindResource("Icon.Check.Round") as Geometry;
                    //    //VerPath.Fill = new SolidColorBrush(Color.FromRgb(0, 200, 0));
                    //}
                    //}));
                }
                catch (Exception ex)
                {
                    //Dispatcher.Invoke(new ThreadStart(delegate
                    //{
                    //    VersionText.Content = FindResource("SearchNewVerErr");
                    //}));
                    Log.Write($"获取版本信息失败：{ex}", LogType.Error);
                }
            }));
            th.Start();
        }
    }
}
